package cn.zcib.film.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Component
@Aspect
@Slf4j
public class AOP {
    @Pointcut("execution(* cn.zcib.film..*.to*(..))")
    public void pointcut01(){}
    @Pointcut("execution(* cn.zcib.film.service.impl..*(..))")
    public void forService(){}

    //仅供controller，仅拦截了左侧菜单请求
    @Around("pointcut01()")
    public Object forIndex(ProceedingJoinPoint joinPoint){
        Object result = null;
        log.info("请求菜单url，检测Session");
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpSession session=attr.getRequest().getSession(true);
        if (session.getAttribute("loginAdmin")==null){
            log.info("尝试请求菜单，已重定向。");
            return "redirect:/main.html";
        }else {
            try {
                log.info("访问了"+joinPoint.getSignature().getName());
                result =joinPoint.proceed();
            } catch (Throwable throwable) {
                log.info(throwable.getMessage());
            }
        }
        return result;
    }

    @AfterReturning(value = "forService()",returning = "res")
    public void AfterReturning(Object res){
            log.info("记录返回值");
            log.info("返回值："+res.toString());
    }

}
